<?php

namespace app\admin\controller\qingdongams\jigoucustomer;

use addons\qingdongams\model\Staff;
use app\admin\controller\qingdongams\Base;
use app\common\library\Auth;
use addons\qingdongams\model\AdminConfig;
use think\Db;
use think\Exception;

/**
 * 公海
 * @desc 操作文档：https://doc.fastadmin.net/qingdongams
 * @desc 软件介绍：https://www.fastadmin.net/store/qingdongams.html
 * @desc 售后微信：qingdong_crm
 */
class Seas extends Base {
	protected $relationSearch = true;
	protected $searchFields   = 'id,name';
	/**
	 * @var \addons\qingdongams\model\Customer
	 */
	protected $model = null;


	public function _initialize() {
		parent::_initialize();
		$this->model = new \addons\qingdongams\model\Customer;
	}


	/**
	 * 查看
	 */
	public function index() {
		$this->request->filter(['strip_tags', 'trim']);
		if ($this->request->isAjax()) {

			list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $order = 'sea_time desc';
            $wheres['is_seas'] =1;
            //手机号是否显示及脱敏
            $phone_auto = AdminConfig::where(['type'=>'seas','field'=>'phone_auto'])->value('value');
            $phone_auty = AdminConfig::where(['type'=>'seas','field'=>'phone_auty'])->value('value');
            //公海权限
            $rules=Staff::getStaffRule('seas');
            $list = $this->model->where($where)->where($wheres)->where("customer_type", "机构")
                ->where(function ($query) use ($rules){
                    foreach ($rules as $rule) {
                        $query->whereOr(['seas_id'=>['like',"%,{$rule},%"]]);
                    }
                })
                ->where('owner_staff_id is null or owner_staff_id = 0')->order($sort, $order) ->paginate($limit);
            foreach($list as $k=>$v){
                $list[$k]['phone_auto'] = false;
                if($phone_auto && $phone_auty){
                    if($phone_auto == 1){
                        $list[$k]['phone_auto'] = true;
                        if($phone_auty == 1 && $v['mobile']){
                            $list[$k]['mobile'] = substr_replace($v['mobile'],'****',3,4);
                        }
                    }
                }
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

			return json($result);
		}
        $phone_auto = AdminConfig::where(['type'=>'seas','field'=>'phone_auto'])->value('value');
		if($phone_auto == 1){
            $visible_param = true;
        }else{
            $visible_param = false;
        }
        $this->assignconfig('visibled',$visible_param);
        $this->view->assign('auths',$this->auth->id);
		return $this->view->fetch();
	}


	/**
	 * 添加
	 */
	public function add() {
		if ($this->request->isPost()) {
			$params = $this->request->post("row/a");
			if ($params) {
				$params = $this->preExcludeFields($params);

				$result = false;
				Db::startTrans();
				try {
					$result = $this->model->allowField(true)->save($params);
					Db::commit();
				} catch (Exception $e) {
					Db::rollback();
					$this->error($e->getMessage());
				}
				if ($result !== false) {
					$this->success();
				} else {
					$this->error(__('No rows were inserted'));
				}
			}
			$this->error(__('Parameter %s can not be empty', ''));
		}
		return $this->view->fetch();
	}

    /**
     * 公海详情
     */
	public function detail($ids=null){
		$row=$this->model->where(['id'=>$ids])->find();
		$this->assign('row',$row);
		$this->assign('ids',$ids);
		return $this->view->fetch();
	}

	/**
	 * 删除
	 */
	public function del($ids = "") {
		if (!$this->request->isPost()) {
			$this->error(__("Invalid parameters"));
		}
		$ids                 = $ids ? $ids : $this->request->post("ids");
		$row                 = $this->model->get($ids);
		$this->modelValidate = true;
		if (!$row) {
			$this->error(__('No Results were found'));
		}
		Auth::instance()->delete($row['id']);
		$this->success();
	}
}
